package com.huewu.pla.pinterest;

import java.io.Serializable;
import java.util.List;

import opens.components.http.HttpObjectRequest;
import opens.components.http.core.RequestQueue;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.huewu.pla.lib.MultiColumnPullToRefreshListView;
import com.huewu.pla.lib.internal.PLA_AdapterView;
import com.huewu.pla.lib.internal.PLA_AdapterView.OnItemClickListener;
import com.huewu.pla.pinterest.internal.ImageWrapper;
import com.huewu.pla.pinterest.internal.SimpleViewBuilder;
import com.idreems.model.JionServerData;
import com.idreems.model.ServerDataDecoder;
import com.idreems.zakerlike.MyApplication;
import com.idreems.zakerlike.R;
import com.lurencun.android.adapter.AbstractAdapter;
import com.lurencun.android.adapter.CommonAdapter;

public class PullToRefreshPinterestActivity extends Activity {
    private MultiColumnPullToRefreshListView mPulltoRefreshListView = null;

    JionServerData mJsonKeyGroup;
    ProgressBar mBar;

    protected AbstractAdapter<ImageWrapper> mAdapter = null;
    public static final int PULL_TO_REFRESH_ID = 1008611;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_pull_to_refresh_pinterest);

        Intent intent = getIntent();
        if (null != intent) {
            Serializable obj = intent.getSerializableExtra(ServerDataDecoder.kJsonkeyWrapper);
            if (obj instanceof JionServerData) {
                mJsonKeyGroup = (JionServerData) obj;
            }
        }
        if (null == mJsonKeyGroup) {
            mJsonKeyGroup = new JionServerData();
        }
        mBar = (ProgressBar) findViewById(R.id.progessbar);
        mBar.setIndeterminate(true);
        mBar.setVisibility(View.VISIBLE);

        mPulltoRefreshListView = (MultiColumnPullToRefreshListView) findViewById(R.id.list);
        reload();
        // mPulltoRefreshListView.setOnRefreshListener(new OnRefreshListener() {
        // @Override
        // public void onRefresh() {
        // reload();
        // }
        // });

        mPulltoRefreshListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(PLA_AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), PhotoViewActivity.class);
                ImageWrapper wrapper = (ImageWrapper) parent.getItemAtPosition(position);

                if (null != wrapper && !TextUtils.isEmpty(wrapper.largeUrl)) {
                    intent.putExtra(PhotoViewActivity.kURL, wrapper.largeUrl);

                    if (view instanceof ViewGroup) {
                        ViewGroup viewGroup = (ViewGroup) view;
                        // find imageview
                        for (int i = 0; i < viewGroup.getChildCount(); i++) {
                            View v = viewGroup.getChildAt(i);
                            if (v instanceof ImageView) {
                                ImageView imageView = ((ImageView) v);
                                BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable());
                                if (null != bitmapDrawable) {
                                    intent.putExtra(PhotoViewActivity.kPlaceholderImage, bitmapDrawable.getBitmap());
                                    break;
                                }
                            }
                        }

                    }
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

    protected void reload() {
        TimelineRequest request = new TimelineRequest();
        request.get(mJsonKeyGroup.url(1));
        request.setCache(MyApplication.getCache());
        request.onSuccess(this, "onTimelineRequestSuccess");
        RequestQueue.instance().push(request);
    }

    public class TimelineRequest extends HttpObjectRequest<List<ImageWrapper>> {

        @Override
        protected void onHttpResponseReceived(HttpResponse response) throws Exception {
            List<ImageWrapper> timeline = mJsonKeyGroup.decode(getString(response));
            setResponseObject(timeline);
        }
    }

    @SuppressWarnings("unused")
    private void onTimelineRequestSuccess(TimelineRequest request) {
        List<ImageWrapper> timeline = request.getResponseObject();

        mAdapter = new CommonAdapter<ImageWrapper>(getLayoutInflater(), new SimpleViewBuilder());
        mAdapter.update(timeline);
        mPulltoRefreshListView.setAdapter(mAdapter);
        mPulltoRefreshListView.onRefreshComplete();

        mBar.setVisibility(View.GONE);
    }

}// end of class
